from transitions import Machine
from datetime import datetime, time

class WaterHeater:
    states = ['off', 'on', 'wrong', 'sleep']

    def __init__(self):
        self.machine = Machine(model=self, states=WaterHeater.states, initial='off')

        # 开始加热条件：同时满足有水，水温低
        self.machine.add_transition('start_heating', 'off', 'on',
                                    conditions=['is_water_available', 'is_water_cool'],
                                    unless=['is_wrong'])
        # 停止加热条件：没水、不是白天、温度过高满足其一
        self.machine.add_transition('stop_heating', 'on', 'off',
                                    conditions=['is_overheated'],
                                    unless=['is_wrong'])
        # 除非坏了，否则直接进入wrong
        self.machine.add_transition('wrong', 'on', 'wrong', unless=['is_wrong'])
        # 除非不是白天，否则关闭
        self.machine.add_transition('shutdown1', 'on', 'sleep', unless=['is_morning'])
        self.machine.add_transition('shutdown2', 'on', 'sleep', unless=['is_morning'])
        #
        self.machine.add_transition('wake_up', 'sleep', 'off',
                                    conditions=['is_morning'],
                                    unless=['is_wrong'])
        self.temperature = 0
        self.water_level = 0

    def is_wrong(self):
        return self.state == 'wrong'

    def is_water_available(self):
        return self.water_level > 0

    def is_water_cool(self):
        return self.temperature < 20

    def is_overheated(self):
        return self.temperature > 100

    def is_morning(self):
        now = datetime.now().time()
        return time(7, 0) <= now <= time(23, 0)

    # 设置水温和水位
    def set_temperature(self, temperature):
        self.temperature = temperature

    def set_water_level(self, water_level):
        self.water_level = water_level

if __name__ == '__main__':
    heater = WaterHeater()
    print('---初始化---')
    print(f"temperature:{heater.temperature}, water_level: {heater.water_level}, Current State:", heater.state)
    heater.set_temperature(10)
    heater.set_water_level(1)
    heater.start_heating()
    print('---加热---')
    print(f"temperature:{heater.temperature}, water_level: {heater.water_level}, Current State:", heater.state)

    heater.set_water_level(0)
    try:
        heater.start_heating()
    except:
        print('不符合加热条件')

    # Simulate temperature reaching 101 degrees
    heater.set_temperature(101)
    print(f"temperature:{heater.temperature}, water_level: {heater.water_level}, Current State:", heater.state)
    heater.stop_heating()
    print('---停止加热---')
    print(f"temperature:{heater.temperature}, water_level: {heater.water_level}, Current State:", heater.state)

    # simulate waking up
    heater.set_temperature(0)
    heater.set_water_level(1)
    try:
        heater.wake_up()
        print('---唤醒---')
    except:
        print('不符合唤醒条件')
    print(f"temperature:{heater.temperature}, water_level: {heater.water_level}, Current State:", heater.state)
    # Simulate going to sleep
    try:
        heater.shutdown1()
    except:
        print('当前是工作时间，无法关闭')
    print(f"temperature:{heater.temperature}, water_level: {heater.water_level}, Current State:", heater.state)
    try:
        heater.shutdown2()
    except:
        print('当前是工作时间，无法关闭')
    print(f"temperature:{heater.temperature}, water_level: {heater.water_level}, Current State:", heater.state)
    heater.start_heating()
    heater.set_temperature(200)
    # Simulate the heater breaking down
    print('---模拟烧坏---')
    heater.wrong()
    print(f"temperature:{heater.temperature}, water_level: {heater.water_level}, Current State:", heater.state)


